﻿using System;
using System.Drawing;

namespace PlantsZombies.Common.Bullet
{
    [PlantZombie(SpeciesType.Sunflower)]
    public unsafe class SunflowerBullet : BulletObject
    {
        public SunflowerBullet(ImageObject[] images, GameObject gameObject)
            : base(new Size(50, 50), images, gameObject)
        {

        }
        DateTime lastDate = DateTime.MinValue;
        public override void Attack(ListGameObject* ptr)
        {
            if (lastDate == DateTime.MinValue)
            {
                lastDate = DateTime.Now;
            }
            else
            {
                if ((DateTime.Now - lastDate).TotalSeconds >= 8)
                {
                    _bullets.Add(new BulletPoint
                    {
                        Index = 0,
                        X = _gameObject.Location.X + 20,
                        Y = _gameObject.Location.Y,
                    });
                    lastDate = DateTime.Now;
                }
            }
            //if (_bullets.Count > 3)
            //    _bullets.RemoveAt(0);
        }
        private float _bulletFactor = 0.1f;
        private void MoveSunflowerBullet(Point point, BulletObject bulletObject)
        {
            if (bulletObject == null) return;
            for (int i = 0; i < bulletObject.BulletObjects.Count; i++)
            {
                var p = bulletObject.BulletObjects[i];
                p.X = (int)((_bulletFactor * point.X + p.X) / (1 + _bulletFactor));
                p.Y = (int)((_bulletFactor * point.Y + p.Y) / (1 + _bulletFactor));
                if (p.Y <= point.Y)
                {
                    bulletObject.BulletObjects.RemoveAt(i);
                    //MouseDownCallback();
                }
            }
        }
        public override void Move(ListGameObject* ptr)
        {
            if (_imagesBullet != null)
            {
                int maxHeight = _gameObject.MaxRow * _gameObject.CellHeight + _gameObject.StartY;
                foreach (var image in _bullets)
                {
                    image.Y += 5;
                    if (image.Y + BulletSize.Height > maxHeight)
                    {
                        image.Y = maxHeight - (BulletSize.Height >> 1);
                        image.AutoRemove = true;
                    }
                }
            }
        }
    }
}
